home *** CD-ROM | disk | FTP | other *** search
- //--------------------------------------------
- // OwnerDrawMenu.cs ⌐ 2001 by Charles Petzold
- //--------------------------------------------
- using System;
- using System.Drawing;
- using System.Drawing.Text; // Para la enumeraci≤n HotkeyPrefix
- using System.Windows.Forms;
-
- class OwnerDrawMenu: Form
- {
- const int iFontPointSize = 18; // Para los elementos de men·
- MenuItem miFacename;
-
- public static void Main()
- {
- Application.Run(new OwnerDrawMenu());
- }
- public OwnerDrawMenu()
- {
- Text = "Men· dibujado por el propietario";
-
- // Elementos de nivel superior
-
- Menu = new MainMenu();
- Menu.MenuItems.Add("&Nombres de fuente");
-
- // Matriz de elementos del submen·
-
- string[] astrText = {"&Times New Roman", "&Arial", "&Courier New"};
- MenuItem [] ami = new MenuItem[astrText.Length];
-
- EventHandler ehOnClick = new EventHandler(MenuFacenameOnClick);
- MeasureItemEventHandler ehOnMeasureItem =
- new MeasureItemEventHandler(MenuFacenameOnMeasureItem);
- DrawItemEventHandler ehOnDrawItem =
- new DrawItemEventHandler(MenuFacenameOnDrawItem);
-
- for (int i = 0; i < ami.Length; i++)
- {
- ami[i] = new MenuItem(astrText[i]);
- ami[i].OwnerDraw = true;
- ami[i].RadioCheck = true;
- ami[i].Click += ehOnClick;
- ami[i].MeasureItem += ehOnMeasureItem;
- ami[i].DrawItem += ehOnDrawItem;
- }
- miFacename = ami[0];
- miFacename.Checked = true;
-
- Menu.MenuItems[0].MenuItems.AddRange(ami);
- }
- void MenuFacenameOnClick(object obj, EventArgs ea)
- {
- miFacename.Checked = false;
- miFacename = (MenuItem) obj;
- miFacename.Checked = true;
-
- Invalidate();
- }
- void MenuFacenameOnMeasureItem(object obj, MeasureItemEventArgs miea)
- {
- MenuItem mi = (MenuItem) obj;
- Font font = new Font(mi.Text.Substring(1), iFontPointSize);
-
- StringFormat strfmt = new StringFormat();
- strfmt.HotkeyPrefix = HotkeyPrefix.Show;
-
- SizeF sizef = miea.Graphics.MeasureString(mi.Text, font,
- 1000, strfmt);
-
- miea.ItemWidth = (int) Math.Ceiling(sizef.Width);
- miea.ItemHeight = (int) Math.Ceiling(sizef.Height);
-
- miea.ItemWidth += SystemInformation.MenuCheckSize.Width *
- miea.ItemHeight /
- SystemInformation.MenuCheckSize.Height;
-
- miea.ItemWidth -= SystemInformation.MenuCheckSize.Width;
- }
- void MenuFacenameOnDrawItem(object obj, DrawItemEventArgs diea)
- {
- MenuItem mi = (MenuItem) obj;
- Graphics grfx = diea.Graphics;
- Brush brush;
-
- // Crea la fuente y el formato de la cadena.
-
- Font font = new Font(mi.Text.Substring(1), iFontPointSize);
- StringFormat strfmt = new StringFormat();
- strfmt.HotkeyPrefix = HotkeyPrefix.Show;
-
- // Calcula los rectßngulos de marca de verificaci≤n y texto.
-
- Rectangle rectCheck = diea.Bounds;
-
- rectCheck.Width = SystemInformation.MenuCheckSize.Width *
- rectCheck.Height /
- SystemInformation.MenuCheckSize.Height;
-
- Rectangle rectText = diea.Bounds;
-
- rectText.X += rectCheck.Width;
-
- // Realiza todo el dibujo.
-
- diea.DrawBackground();
-
- if ((diea.State & DrawItemState.Checked) != 0)
- ControlPaint.DrawMenuGlyph(grfx, rectCheck, MenuGlyph.Bullet);
-
- if ((diea.State & DrawItemState.Selected) != 0)
- brush = SystemBrushes.HighlightText;
- else
- brush = SystemBrushes.FromSystemColor(SystemColors.MenuText);
-
- grfx.DrawString(mi.Text, font, brush, rectText, strfmt);
- }
- protected override void OnPaint(PaintEventArgs pea)
- {
- Graphics grfx = pea.Graphics;
- Font font = new Font(miFacename.Text.Substring(1), 12);
- StringFormat strfmt = new StringFormat();
- strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
-
- grfx.DrawString(Text, font, new SolidBrush(ForeColor), 0, 0);
- }
- }
-